package no.hib.tod141.hasj.customLibs;
import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class ShortText extends SimpleTagSupport {
	
	private int maxchars = 10;
	
	public void doTag() throws JspException , IOException{
		try {
        	StringWriter sw = new StringWriter();
        	getJspBody().invoke(sw);
        	JspWriter out = getJspContext().getOut();
        	
        	//kode for å korte ned
        	if (sw.toString().length() <= maxchars) {
        		out.println(sw.toString());
        	}
        	else {
        		String shortText = sw.toString().substring(0, getMaxchars());
        		out.println(shortText);
        	}
        	
        	
	   } catch (Exception e){
	    	throw new Error("Something did not go as planned...");
	   }
		
	}

	public int getMaxchars() {
		return maxchars;
	}

	public void setMaxchars(int maxchars) {
		this.maxchars = maxchars;
	}

}
